package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class SetmealServiceimpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    @Override
    public PageResult page(SetmealPageQueryDTO setmealDTO) {
        PageHelper.startPage(setmealDTO.getPage(),setmealDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealDTO);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public SetmealVO getbyid(Long id) {
        SetmealVO setmealVO =setmealMapper.getbyid(id);
        setmealVO.setSetmealDishes(setmealDishMapper.getdishsbyid(id));
        return setmealVO;
    }

    @Override
    public void status(Integer status,Long id) {
        SetmealVO setmealVO = getbyid(id);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealMapper.insert(setmeal);
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public void delete( List<Long> ids) {
        for (Long id : ids) {
            SetmealVO s = setmealMapper.getbyid(id);
            if(s.getStatus()==1) throw new DeletionNotAllowedException(s.getName()+ "起售的套餐不允许删除");
        }
        setmealDishMapper.delete(ids);
        setmealMapper.delete(ids);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        setmealDishMapper.deletebysetmealid(setmealDTO.getId());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
